package com.server.admin.controller;


import com.server.base.dto.AdminLoginDto;
import com.server.base.entity.SysAdmin;
import com.server.base.service.ISysAdminService;
import com.server.base.vo.AdminLoginSuccessVO;
import com.server.common.annotation.RepeatSubmit;
import com.server.common.api.ApiResult;
import com.server.logs.annotation.LogRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author zgr
 * @since 2023-07-11
 */
@RestController
@Api(value = "SysAdminController", tags = "管理员")
public class SysAdminController {

    private final ISysAdminService sysAdminService;

    public SysAdminController(ISysAdminService sysAdminService) {
        this.sysAdminService = sysAdminService;
    }

    @PostMapping("/admin/login")
    @ApiOperation(value = "登录")
    public ApiResult login(@RequestBody AdminLoginDto adminLoginDto) throws UnsupportedEncodingException {
        AdminLoginSuccessVO vo = sysAdminService.login(adminLoginDto);
        return ApiResult.success(vo);
    }

    @GetMapping("/admin/logout")
    @ApiOperation(value = "退出")
    public ApiResult logout() throws UnsupportedEncodingException {
        Long id = null;
        return ApiResult.status(sysAdminService.logout(id));
    }

    @GetMapping("/admin/search/{id}")
    @ApiOperation(value = "搜索")
    @RepeatSubmit
    @LogRecord(content = "搜索用户%s", value = "id")
    public ApiResult search(@PathVariable Long id) {
        SysAdmin entity = sysAdminService.getById(id);
        return ApiResult.success(entity);
    }
}

